package com.swsc.solarverse.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.swsc.solarverse.common.Constant;
import com.swsc.solarverse.dao.entity.DefectDefinitionDO;
import com.swsc.solarverse.dao.mapper.DefectDefinitionMapper;
import com.swsc.solarverse.service.IDefectDefinitionService;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author AutoGenerator
 * @since 2023-03-14
 */
@Service
public class DefectDefinitionServiceImpl extends ServiceImpl<DefectDefinitionMapper, DefectDefinitionDO> implements IDefectDefinitionService {

    @Override
    public List<DefectDefinitionDO> listDefectDefinitions(Collection<String> defectCategoryIn) {
        return listDefectDefinitions(null, null, defectCategoryIn);
    }

    @Override
    public List<DefectDefinitionDO> listDefectDefinitions(Integer tenantNo, Integer organNo, Collection<String> defectCategoryIn) {
        return baseMapper.selectList(Wrappers.lambdaQuery(DefectDefinitionDO.class)
                .eq(null != tenantNo, DefectDefinitionDO::getTenantNo, tenantNo)
                .eq(DefectDefinitionDO::getOrganNo, (null == organNo ? Constant.DEFAULT_SYS_ORGAN_NO : organNo))
                .in(CollectionUtils.isNotEmpty(defectCategoryIn), DefectDefinitionDO::getDefectCategory, defectCategoryIn)
                .orderByAsc(DefectDefinitionDO::getDefectLevel));
    }

    @Override
    public Map<String, DefectDefinitionDO> getDefectDefinitionMap(String defectCategory) {
        return getDefectDefinitionMap(null, null, defectCategory);
    }

    @Override
    public Map<String, DefectDefinitionDO> getDefectDefinitionMap(Integer tenantNo, Integer organNo, String defectCategory) {
        List<DefectDefinitionDO> defectDefinitionDos = listDefectDefinitions(tenantNo, organNo, StringUtils.isNotBlank(defectCategory) ? Arrays.asList(defectCategory) : Collections.EMPTY_LIST);

        if (CollectionUtils.isNotEmpty(defectDefinitionDos)) {
//            return defectDefinitionDos.stream().collect(Collectors.toMap(DefectDefinitionDO::getDefectName, Function.identity(), (k1, k2) -> k2));
            return defectDefinitionDos.stream().collect(Collectors.toMap(d -> d.getDefectCategory().concat("-").concat(d.getDefectName()), Function.identity(), (k1, k2) -> k2));
        }
        return Collections.emptyMap();
    }
}
